// pages/product-add/product-add.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    type:'', //是否为编辑
    proType: [], //类型
    proIdx: '', //类型序号
    add_product_img: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id||'';
    var type = options.type ||'';
    this.setData({
      id,type
    })
    // this.getProductType(); //获取产品类型
    if (type=='update'){
      this.getProductDetail(); //获取产品详情
    }
  },


  getProductDetail() { //获取产品详情
    var that = this;
    wx.request({
      url: app.globalData.host + 'product/productDetail',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        id: that.data.id
      },
      success(res) {
        console.log(res)
        if (res.data.data.detail.banner) {
          var list = res.data.data.detail.banner
          var list2 = list.split(',')
        } else {
          var list2 = [];
        }
        var detail = res.data.data.detail
        var proType = that.data.proType;
        proType.map((v,k)=>{
          if(v.id==detail.cate_id){
            that.data.proIdx = k;
          }
        })
        console.log(proType)
        console.log(that.data.proIdx)
        that.setData({
          proIdx: that.data.proIdx,
          detail: detail,
          add_product_img: list2,
        })
      },
      fail(err) { },
      complete(comp) { },
    })

  },

  getProductType(){ //获取类型
    var that = this;
    wx.request({
      url: app.globalData.host + 'product/productEdit',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      data: {
        // id:that.data.id,
        // user_id:wx.getStorageSync('userId')
      },
      success(res) {
        console.log(res)
        var proType = res.data.data.category;
        that.setData({
          proType
        })
      },
      fail(err) { },
      complete(comp) { },
    })

  },

  bindPickerChange: function(e) { //选择类型
    let that = this;
    let idx = e.detail.value;
    that.setData({
      proIdx: idx
    })
  },

  //添加图片
  chooseIMG: function(e) {
    let that = this;
    let { add_product_img } = that.data;

    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePaths可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;

        wx.uploadFile({
          url: app.globalData.host + 'common/uploadimg',
          filePath: tempFilePaths[0],
          name: 'file',
          success: function(res) {
            console.log(res)
            res = JSON.parse(res.data);
            add_product_img.push(res.filesrc)

            that.setData({
              add_product_img,
            })
          },
          fail: function(res) {},
          complete: function(res) {},
        })
        console.log(that.data.add_product_img)
      },
    })
  },

  removePhoto: function(e) { //移除图片
    let that = this;
    let idx = e.currentTarget.dataset.idx
    let { add_product_img } = that.data;

    let list = add_product_img;
    list.splice(idx, 1)
    that.setData({
      add_product_img: list
    })
  },

  submitTap:function(e){ //提交
    console.log(e)
    let that = this;
    let formId = e.detail.formId;
    // if(that.data.id){
    //   var id = that.data.id;
    // }
    let { add_product_img } = that.data;
    // var cate_id = proType[proIdx].id;
    if (e.detail.value.title.length==0){
      wx.showToast({
        title: '请输入产品名称',
        icon: 'none',
      })
      return;
    } else if (add_product_img.length==0){
      wx.showToast({
        title: '请添加产品图片',
        icon: 'none',
      })
      return;
    } else if (e.detail.value.textarea.length==0){
      wx.showToast({
        title: '请输入描述内容',
        icon: 'none',
      })
      return;
    }
    var params = {
      user_id:wx.getStorageSync('userId'),
      title: e.detail.value.title,
      cate_id:'',
      logo: add_product_img[0],
      banner: add_product_img,
      detail: e.detail.value.textarea,
    };
    console.log(params)

    var url = app.globalData.host + 'product/productAdd';
    if(that.data.type=='update'){ //编辑
      params.id = that.data.id;
      url = app.globalData.host + 'product/productEdit';
    }

    app.getFormId(formId);
    app.request(url, params, 'POST', true,
      function (res) {
        console.log(res)
        if(res.data.code==1){
          wx.showToast({
            title: '提交成功',
          })
          if (that.data.type == 'update'){
            setTimeout(function () {
              wx.navigateBack({
                delta: 2,
              })
            }, 1500)
            return;
          }
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1500)
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,
            cancelText: '',
            confirmText: '确认',
            success: function(res) {},
            fail: function(res) {},
            complete: function(res) {},
          })
        }
        

      },
      function(){},
      function(){})
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

})